<?php
class RedisCtrl
{
    //init redis
    static $redisIp = "127.0.0.1";
    static $redisPort =6379;
    static $redisPass ="";
    public $redis = null;

    //Redis
    public function __construct()
    {
        $this->redis = new Redis();
        $this->redis->connect(self::$redisIp,self::$redisPort);
        $this->redis->auth(self::$redisPass);
    }

    public function llen($key)
    {
        $rel = $this->redis->llen($key);
        return $rel;
    }

    public function rpop($key)
    {
        $rel = $this->redis->rpop($key);
        return $rel;
    }

    public function rpoplpush($source,$destination)
    {
        $rel = $this->redis->rpoplpush($source,$destination);
        return $rel;
    }

    public function UpdateClickGoodDataToMysql($data)
    {
        //get id and time from redis list
        $result = json_decode($data,true);
        $id = $result['id'];
        $time = $result['time'];
        $arr = array();

        //like
        $like = $this->redis->zscore("comment:like",$id);
        $like = $like?$like:0;
        //hate
        $hate = $this->redis->zscore("comment:hate",$id);
        $hate = $hate?$hate:0;

        $sql = "update comment_info set like_count=".$like.", hate_count=".$hate." where id=".$id;
        $arr[] = $sql;
        //update sql
        $mysql = new MysqlCtrl();
        $mysql->saveMySQL($arr);

        //更新完，将set集合里需要更新的id去掉
        $this->redis->srem("comment:uploadset",$id);
        //更新完毕，将备份队列里的数据去掉
        $this->redis->lrem("comment:uploadcopylist",$data);

        return $sql."\n";
    }
}